package cn.myfirefly.moming.portal.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import cn.myfirefly.moming.portal.dao.mapper.RoleSysMapper;
import cn.myfirefly.moming.portal.entity.MMRoleSys;
import cn.myfirefly.moming.portal.entity.MMSystemAccess;
import cn.myfirefly.core.BaseDao;
import cn.myfirefly.utils.CollectionUtil;
import cn.myfirefly.utils.StrUtil;

@Repository
public class RoleSysDao extends BaseDao<MMRoleSys> {
	
	@Autowired
	private RoleSysMapper roleSysMapper;
	
	public List<MMSystemAccess> getSystemAccessListByRoleSn(String in) {
		if(StrUtil.isBlank(in)){
			in = "' '";
		}
		return roleSysMapper.getSystemAccessListByRoleSn(in);
	}
	
	public long countBySysId(Integer sn) {
		return roleSysMapper.countBySysId(sn);
	}
	
	public long countRoleFun(Integer roleSn, Integer systemId) {
		return roleSysMapper.countRoleFun(systemId, roleSn);
	}
	
	@SuppressWarnings("unchecked")
	public MMRoleSys get(Integer roleSn, Integer sysId) {
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery(" from MMRoleSys s where s.roleId = ? AND s.sysId = ? ");
		query.setInteger(0, roleSn);
		query.setInteger(1, sysId);
		List<MMRoleSys> list = query.list();
		if (CollectionUtil.isNotEmpty(list)) {
			return list.get(0);
		}
		return null;
	}
}
